package com.example.demo.controller;

import com.example.demo.annotation.UserLoginToken;
import com.example.demo.utils.Result;
import com.example.demo.utils.UploadFile;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@Api("上传图片API")
@RequestMapping("/admin")
public class FileUploadController {

    @UserLoginToken
    @PostMapping("fileUpload")
    public Result fileUpload(@RequestParam(value = "file") MultipartFile file) {

        String path = null;
        try {
            path = UploadFile.uploadByMultipartFile(file);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(-1, e.getMessage());
        }
        if(path.equals("")) {
            return new Result(1, "文件为空");
        }
        return new Result(0, "success", path);
    }

}
